Selecting element by data attribute with jQuery?
Selecting element by data attribute with jQuery?
23831-Jul-2023
Updated on 01-Aug-2023
Home / DeveloperSection / Forums / Selecting element by data attribute with jQuery?
Selecting element by data attribute with jQuery?
Aryan Kumar
01-Aug-2023Sure, here is how to select an element by data attribute with jQuery:
JavaScript
The
element
variable will contain a reference to the element with the data-name attribute of "my-name".Here is an example of how to use this code:
HTML
This code will create two div elements, one with the data-name attribute of "my-name" and one with the data-name attribute of "other-name". The
console.log()
method will print the element with the data-name attribute of "my-name" to the console.